/**
 * @author 
 */
package entity;

import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.NoResultException;
import javax.persistence.Persistence;
import javax.persistence.Query;

/**
 *
 * @author 
 */
public class DaftarLokasi {

    public DaftarLokasi() {
        emf = Persistence.createEntityManagerFactory("itsplaces");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public Lokasi getLokasi(String lokasi) {
        Lokasi lokasitm = null;
        EntityManager em = null;
        try {
            em = getEntityManager();
            Query q = em.createQuery("SELECT count(o) FROM lokasi as o o.lokasi = :lokasi");
            q.setParameter("lokasi", lokasi);
            lokasitm = (Lokasi) q.getSingleResult();
        } catch (NoResultException e) {
        } finally {
            if (em != null) {
                em.close();
            }
        }

        return lokasitm;
    }
    
    public Lokasi getLokasi(Integer id) {
        Lokasi lokasi = null;
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("SELECT object(o) FROM Lokasi AS o WHERE o.id=:id");
            q.setParameter("id", id);
            lokasi = (Lokasi) q.getSingleResult();
           
        } finally {
            if (em != null) {
                em.close();
            }
        }
        return lokasi;
    }
    
    public List<Lokasi> getAllLokasi() {
        List<Lokasi> allLokasi = new ArrayList<Lokasi>();

        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("SELECT object(o) FROM Lokasi AS o");
            allLokasi = q.getResultList();

        } finally {
            if (em != null) {
                em.close();
            }
        }
        return allLokasi;
    }

    public void tambahLokasi(Lokasi lokasi) {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            em.persist(lokasi);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }
}
